// 导入本地存储方法
import { setItem, getItem, removeItem } from '@/utils/storage'

// 定义一个常量用于本地存储数据，防止自己写错
const TOKEN_KEY = 'TOUTIAO_USER'

const state = () => ({
  user: getItem(TOKEN_KEY)
})

const mutations = {
  setUser (state, data) {
    state.user = data
    // 备份数据到本地存储
    setItem(TOKEN_KEY, state.user)
  },
  removeUser (state) {
    state.user = null
    removeItem(TOKEN_KEY)
  }
}

const actions = {}

const getters = {
  token (state) {
    if (state.user) {
      return state.user.token
    } else {
      return ''
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
